local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local HeadSprite = appdf.req(appdf.EXTERNAL_SRC .. "HeadSprite")
local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")
--local cmd = appdf.req(appdf.GAME_SRC..yl.zjh ..".src.models.CMD_Game")

local GameChatLayer = class("GameChatLayer", function()
	local layer = CCLayerColor:create(cc.c4b(0, 0, 0, 0))--cc.Layer:create()
	return layer
end)
local offsetX = 500

function GameChatLayer:ctor(viewParent,userItem,chatTable)
    self.m_parent = viewParent
	self:setName("GameChat")
    self:addTo(self.m_parent,1000)

	--加载csb资源
    local csbNode = ExternalFun.loadCSB("GameChat/GameChat.csb", self)
    csbNode:setPosition(display.width+85,display.cy)
    self.m_csbNode = csbNode


    -- local chatNode = csbNode:getChildByName("chatNode") 
    --按钮回调
	local btcallback = function (ref, type)
		
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
            local tag =  ref:getTag()
			print("GameChatLayer:ctor tag ====="..tag)
			
            if tag == -1 then
                self:showChatView()
            elseif tag == -2 then
                self:showEmojiView()
			elseif tag >200 then	--动画聊天
			
				--appdf.PostCustomEvent("emojiEvent",{viewid = cmd.MY_VIEWID , aniType = tag},self)
                -- self:showEmojiView()
                self.m_csbNode:setPositionX(display.width+85)
                self.beOut =  false
                -- self:removeSelf()
                -- viewParent:sendBiaoQing(userItem.userid)
				if self.m_parent then
						self.m_parent:onSendEmoji(tag)
				end
				
				
            elseif tag > 100 then --文字聊天
			
              --  appdf.PostCustomEvent("chatEvent",{aniType = tag},self)
                -- self:removeSelf()
                -- self:showChatView()
                self.m_csbNode:setPositionX(display.width+85)
                self.beOut =  false
				if self.m_parent then
						self.m_parent:onSendEmoji(tag)
				end
				
            else  
					
					
            end
		end
    end

    local chat_btn = csbNode:getChildByName("chat_btn")
    chat_btn:setTag(-1)
    chat_btn:addTouchEventListener(btcallback)
    self.chat_btn = chat_btn

    local emoji_btn = csbNode:getChildByName("emoji_btn")
    emoji_btn:setTag(-2)
    emoji_btn:addTouchEventListener(btcallback)
    self.emoji_btn = emoji_btn
    self.emoji_btn:loadTextureNormal("GameChat/bq_off.png")


    self.emoji_bg = csbNode:getChildByName("emoji_bg")
    self.chat_bg = csbNode:getChildByName("chat_bg")

    for i=1,12 do
        local btn = self.emoji_bg:getChildByName("emoji"..i)
        btn:setTag(i+200)
        --btn:addTouchEventListener(btcallback)
		
		self:ToBtn(self.emoji_bg,"emoji"..i,function(obj,e)
				if e == ccui.TouchEventType.ended then	
					btcallback(obj,ccui.TouchEventType.ended)
				end
		end)
		
        local size = btn:getContentSize()
        local sp = display.newSprite("Armature/Emoji/bq_"..i..".png")
        sp:addTo(btn)
		sp:setTag(100)
        sp:setPosition(size.width/2,size.height/2)
    end

    local  _chatTable = chatTable or yl.CHAT_LAB_LIST


    local chatcount = (chatTable~=nil and #chatTable > 0) and #chatTable or 8
    for i=1,chatcount do
        local btn = self.chat_bg:getChildByName("chatText"..i)
        btn:setTag(i+100)
      --  btn:setTitleText(yl.CHAT_LAB_LIST[i] or yl.CHAT_LAB_LIST[1])
        btn:addTouchEventListener(btcallback)

		local Text_val = btn:getChildByName("Text_val")
		if Text_val then
            Text_val:setText(_chatTable[i] or _chatTable[1])
		end
    end
    for i=chatcount+1,8 do
        local btn = self.chat_bg:getChildByName("chatText"..i)
        btn:setVisible(false)
    end

	self:ToSpriteOutArea(csbNode,"emoji_bg",function(name,obj) 
		if 	name == "end"  then
				self.m_csbNode:setPositionX(display.width+85)
		end
	end)
	
end

function GameChatLayer:ToBtn(parent, name, callback, attr,tag)
    local btn = parent:getChildByName(name);--seekByName(parent, name);
	if not btn then
        error("ToBtn == nil ", name);
        return;
    end

    if callback then
        btn:addTouchEventListener(function(obj, e)
            if e == ccui.TouchEventType.began then
				--按钮声音统一添加
				--特殊声音通过attr传入
				local t = btn:getChildByTag(100)
				t:setScale(1.10)
			elseif e == ccui.TouchEventType.ended then
				local t = btn:getChildByTag(100)
				t:setScale(1)
				callback(obj, e);	
            end
        end)
    end
end

--区域外响应
function GameChatLayer:ToSpriteOutArea(parent, name, callback,attr)
    local _sprite = parent:getChildByName(name);--seekByName(parent, name);
	if not _sprite then
        error("ToSprite == nil ", name);
        return;
    end

    if callback then
        local onTouchBegin = function (touch, e) 
			local target = e:getCurrentTarget();--获取的当前触摸的目标
			local locationInNode = target:convertToNodeSpace(touch:getLocation());
			local  s = target:getContentSize();
			local  rect = cc.rect(-82, 0, s.width, s.height);        
			if cc.rectContainsPoint(rect,locationInNode) then --判断触摸点是否在目标的范围内
				return false;
			else
				return true;
			end
		end
		
        local onTouchMove = function (touch, e)
		end
		
        local onTouchEnd = function (touch, e)
			callback("end",_sprite); 
		end
		
        local listent = cc.EventListenerTouchOneByOne:create();
        listent:registerScriptHandler(onTouchBegin,cc.Handler.EVENT_TOUCH_BEGAN);
        listent:registerScriptHandler(onTouchMove,cc.Handler.EVENT_TOUCH_MOVED);
        listent:registerScriptHandler(onTouchEnd,cc.Handler.EVENT_TOUCH_ENDED);
        local eventDispatcher = _sprite:getEventDispatcher();
        eventDispatcher:addEventListenerWithSceneGraphPriority(listent,_sprite);
    end
end

function GameChatLayer:showView(GameType)
	self.m_csbNode:setPositionX(display.width-offsetX)
	if GameType == yl.zjh then --特殊处理，只有表情
		 local 	chat_btn = self.m_csbNode:getChildByName("chat_btn")
		chat_btn:setVisible(false)

		chat_btn = self.m_csbNode:getChildByName("emoji_btn")
		chat_btn:setVisible(false)

		self:showEmojiView()
	end

end

function GameChatLayer:showChatView()
--	local key = "chat"..GlobalUserItem.nCurGameKind
--	cc.UserDefault:getInstance():getIntegerForKey(key,1)
    self.chat_bg:setVisible(true)
    self.emoji_bg:setVisible(false)
    self.chat_btn:loadTextureNormal("GameChat/chat_on.png")
    self.emoji_btn:loadTextureNormal("GameChat/bq_off.png")
    if not self.beOut then
        --self.m_csbNode:setPositionX(display.width-offsetX)
        self.beOut =  true
    end
end
  
function GameChatLayer:showEmojiView()
	--local key = "chat"..GlobalUserItem.nCurGameKind
	
	
	--cc.UserDefault:getInstance():getIntegerForKey(key,0)
    self.chat_bg:setVisible(false)
    self.emoji_bg:setVisible(true)
    self.chat_btn:loadTextureNormal("GameChat/chat_off.png")
    self.emoji_btn:loadTextureNormal("GameChat/bq_on.png")
    if not self.beOut then
        --self.m_csbNode:setPositionX(display.width-offsetX)
        self.beOut =  true
    end
end

return GameChatLayer